package cn.itcast.travel.web.servlet;

import cn.itcast.travel.domain.Order;
import cn.itcast.travel.domain.PayController;
import cn.itcast.travel.domain.Route;
import cn.itcast.travel.domain.User;
import cn.itcast.travel.service.PayService;
import cn.itcast.travel.service.RouteService;
import cn.itcast.travel.service.impl.PayServiceImp;
import cn.itcast.travel.service.impl.RouteServiceImp;
import cn.itcast.travel.util.DateUtils;
import cn.itcast.travel.util.JedisUtil;
import com.alipay.api.AlipayApiException;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

@WebServlet("/money/*")
public class MoneyServlet extends BaseServlet {

    private PayController p = new PayController();
    private RouteService routeService=new RouteServiceImp();
    private PayService payService=new PayServiceImp();
    public void ply(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String out_trade_no = UUID.randomUUID().toString();
        String rid = request.getParameter("rid");
        Jedis jedis = JedisUtil.getJedis();
        jedis.set("payRouteRid",rid);
        jedis.expire("payRouteRid",60);
        User login =(User) JedisUtil.getRedisObject(request.getSession().getId());
        Route one = routeService.findOne(rid);

        //付款金额，必填
        String total_amount =Integer.toString( (int)one.getPrice());
        //订单名称，必填
        String subject =one.getRname();
        //商品描述，可空
        String body = one.getRouteIntroduce();
        Order order=new Order();
       order.setUser(login);
        order.setIspay("否");
        order.setTime(DateUtils.getCurrentTime());
        order.setOut_trade_no(out_trade_no);
      order.setRoute(one);
        payService.addOrder(order);
        p.alipay(response,out_trade_no,total_amount,subject,body);


    }

    public void returnPly(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, AlipayApiException {
        String s = p.returnUrl(request, response);
        if(s.equals("no")){
            //支付失败
            System.out.println("支付失败");
            response.sendRedirect(request.getContextPath()+"/route_detail.html?rid="+JedisUtil.getJedis().get("payRouteRid"));
        }else {
            //支付成功
            System.out.println("支付成功");
            response.sendRedirect(request.getContextPath()+"/route_detail.html?rid="+JedisUtil.getJedis().get("payRouteRid"));
        }
    }

    /**
     * 重新支付的方法
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void ply2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String out_trade_no = request.getParameter("out_trade_no");
        PayController pc = new PayController();
        Route route = routeService.findByOut_trade_no(out_trade_no);
        pc.alipay2(response,out_trade_no,route.getRname(),route.getRouteIntroduce());
        //pc.alipay2(response,);
    }

}
